package cn.yuhe.org.service.impl;

import cn.yuhe.basic.exception.BusinessException;
import cn.yuhe.basic.utils.BaiduAuditUtils;
import cn.yuhe.org.domain.Employee;
import cn.yuhe.org.domain.Shop;
import cn.yuhe.org.domain.ShopAuditLog;
import cn.yuhe.org.mapper.EmployeeMapper;
import cn.yuhe.org.mapper.ShopAuditLogMapper;
import cn.yuhe.org.mapper.ShopMapper;
import cn.yuhe.org.service.IEmployeeService;
import cn.yuhe.org.service.IShopService;
import cn.yuhe.basic.service.impl.BaseServiceImpl;
import cn.yuhe.org.vo.ShopEchartVo;
import jdk.nashorn.internal.ir.WhileNode;
import org.apache.xmlbeans.impl.xb.xsdschema.Wildcard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-07-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired // 业务高度封装，调用其他类的service
    private IEmployeeService employeeService;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;


    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        // 1,判断参数是否为空
        if(StringUtils.isEmpty(shop.getName()) 
                || StringUtils.isEmpty(shop.getAdmin().getUsername()) 
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress()) 
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword()) 
                || StringUtils.isEmpty(shop.getAdmin().getPassword()) 
                || StringUtils.isEmpty(shop.getAdmin().getEmail())){
            throw new BusinessException("注册信息为空，请重新输入！");
        }


        // 2.判断两次密码是否一样
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            // 不一样就创建一个自定义异常
            throw new BusinessException("两次密码不一样，请重新输入！");
        }

        // 调用百度Ai判断参数是否合规
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("请规范店铺名称");
        }


        // 查询店铺是否纯在
        Shop loadShopTmp = shopMapper.loadByName(shop.getName());
        if (loadShopTmp != null){
            throw new BusinessException("店铺已注册，请更换店铺信息！");
        }
        // 4.管理员以注册，且有店铺，抛出异常
        Employee empTmp = employeeService.loadByUserName(admin.getUsername());
        if (empTmp != null && empTmp.getShopId() != null){
            throw new BusinessException("该管理员已绑定店铺，请更换其他管理员！");
        }
        // 5.管理员没注册，注册管理员
        if (empTmp == null){
            employeeService.save(admin);
            empTmp = admin;
        }

        // 设置到店铺的外键
        shop.setAdminId(empTmp.getId());
        // 保存店铺,一定要返回主键
        shopMapper.save(shop);
        //将店铺的主键设置到管理员的外键
        empTmp.setShopId(shop.getId());
        //修改员工数据
        employeeService.update(empTmp);
    }

    /**
    * @Title: reject
    * @Description: 驳回店铺
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/7/2 13:08
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        // 获取前台的shopId，转换为long
        long l = shopAuditLog.getShopId().longValue();
        // 根据获取的shopid查询shop对象赋值state状态
        Shop shop = shopMapper.loadById(l);
        shop.setState(-1);
        shopMapper.update(shop);
        shopAuditLog.setAuditId(8888);
        // 将前台信息添加数据库
        shopAuditLogMapper.save(shopAuditLog);
        // 发送邮件通知商家
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2691662354@qq.com");
            helper.setSubject("宠物乐园店铺入住结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲！您的店铺入驻申请信息已收到，消息内容存在不合规，请重新填写</h1>"+
                    "店铺信息修改链接<a href='http://localhost:8081/#/Register2?shopId="+shopAuditLog.getShopId()+"'>点我修改店铺信息</a>",true);
            // 4.添加附件
            helper.addAttachment("好东西.jpg",new File("C:\\Users\\Administrator\\Desktop\\景\\4.jpg"));
            // 5.设置收件人
            helper.setTo("2691662354@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
    * @Title: pass
    * @Description: 通过店铺
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/7/2 13:08
    * @Parameters: [shopAuditLog]
    * @Return void
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        // 获取前台的shopId，转换为long
        long l = shopAuditLog.getShopId().longValue();
        // 根据获取的shopid查询shop对象赋值state状态
        Shop shop = shopMapper.loadById(l);
        shop.setState(1);
        shopMapper.update(shop);
        // 还未设置登录用户，统一8888
        shopAuditLog.setAuditId(8888);
        // 将前台信息添加数据库
        shopAuditLogMapper.save(shopAuditLog);
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2691662354@qq.com");
            helper.setSubject("宠物乐园店铺入住结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲！您的店铺入驻申请信息已通过</h1>"+
                    "店铺激活链接<a href='http://localhost:8081/shop/use?shopId="+shopAuditLog.getShopId()+"'>点我激活店铺</a>",true);
            // 4.添加附件
            helper.addAttachment("好东西.jpg",new File("C:\\Users\\Administrator\\Desktop\\景\\4.jpg"));
            // 5.设置收件人
            helper.setTo("2691662354@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
    * @Title: loadEcharts
    * @Description: 查询店铺数据报表
    * @Author: Mr.Qiu
    * @Version: 1.0
    * @Date:  2022/7/2 22:22
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @Override
    public Map<String, Object> loadEcharts() {
        List<ShopEchartVo> shopEchartVos = shopMapper.loadEcharts();
        ArrayList<String> states = new ArrayList<>();
        ArrayList<Integer> counts = new ArrayList<>();
        String A = "";
        for (ShopEchartVo shopEchartVo : shopEchartVos) {
            // 这儿进行判断是几，放入集合中字符
            switch(shopEchartVo.getState()){
                case 0: A = "待审核";break;
                case 1: A = "以审核";break;
                case -1:A = "驳回";break;
                case 2:A = "待激活";break;
            }
            states.add(A);
            counts.add(shopEchartVo.getCount());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",counts);
        return map;
    }

    @Override
    public void refuse(ShopAuditLog shopAuditLog) {
        // 获取前台的shopId，转换为long
        long l = shopAuditLog.getShopId().longValue();
        // 根据获取的shopid查询shop对象赋值state状态
        Shop shop = shopMapper.loadById(l);
        shop.setState(0);
        shopMapper.update(shop);
        shopAuditLog.setAuditId(8888);
        // 将前台信息添加数据库
        shopAuditLogMapper.save(shopAuditLog);
        try {
            // 1.创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("2691662354@qq.com");
            helper.setSubject("宠物乐园店铺入住结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>亲！您的店铺入驻申请信息未通过，请重新更换管理员重试</h1>"+
                    "店铺重新申请链接<a href='http://localhost:8081/#/ShopRegister'>点我申请店铺</a>",true);
            // 5.设置收件人
            helper.setTo("2691662354@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void batchAdd(List<Shop> shops) {
        shopMapper.batchAdd(shops);

    }

}
